package com.gjy.redis.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-05 09:09:14
 */
@Configuration
public class RedisConfig {

    /*private static final String REDIS_HOST_PORT = "192.168.253.142:6379";
    private static final String REDIS_HOST = "192.168.253.142";*/
    private static final String REDIS_HOST_PORT = "127.0.0.1:6379";
    private static final String REDIS_HOST = "127.0.0.1";
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return template;
    }

    @Bean
    public RedissonClient redisson1() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + REDIS_HOST_PORT);
        config.setLockWatchdogTimeout(60_000);
        return Redisson.create(config);
    }

    @Bean
    public static Jedis jedis() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(200);
        config.setMaxTotal(300);
        config.setTestOnBorrow(false);
        config.setTestOnReturn(false);
        JedisPool pool = new JedisPool(config, REDIS_HOST, 6379, 3000);
        return pool.getResource();
    }

    @Value("${spring.redis.host:localhost}")
    private String host;

    @Value("${spring.redis.port:6379}")
    private int port;

    @Value("${spring.redis.password:}")
    private String password;

    @Value("${spring.redis.timeout:2000}")
    private int timeout;

    @Bean
    public JedisPool jedisPool() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(100);
        poolConfig.setMaxIdle(50);
        poolConfig.setMinIdle(10);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);

        if (password != null && !password.trim().isEmpty()) {
            return new JedisPool(poolConfig, host, port, timeout, password);
        } else {
            return new JedisPool(poolConfig, host, port, timeout);
        }
    }

}
